const { app, BrowserWindow, ipcMain, globalShortcut, Menu } = require("electron");
const fs = require("fs");
const path = require("path");

let hostPath = {
    win32: "C:\\Windows\\System32\\drivers\\etc\\hosts",
    mas: "~\\private\\etc\\hosts",
};

let hostData = {
    1: {
        data: [
            {
                ip: "127.0.0.1",
                web: "localhost",
                enable: true,
            },
        ],
        name: "local",
        enable: true,
        isSelect: true,
    },
};

function changeHost(data, k = false) {
    let writeFileData = "";
    if (k) {
        if (typeof k == "array") {
            k.map((i) => {
                if (data[i].enable) {
                    data[i].data.map((item) => {
                        if (item.enable) {
                            writeFileData += `${item.ip} ${item.web}\n`;
                        }
                    });
                }
            });
        } else {
            if (data[k].enable) {
                data[k].data.map((item) => {
                    if (item.enable) {
                        writeFileData += `${item.ip} ${item.web}\n`;
                    }
                });
            }
        }
    } else {
        hostData = data;
        for (let key in data) {
            if (data[key].enable) {
                data[key].data.map((item) => {
                    if (item.enable) {
                        writeFileData += `${item.ip} ${item.web}\n`;
                    }
                });
            }
        }
    }
    fs.writeFileSync(hostPath[process.platform], writeFileData);
}

function clearHost() {
    fs.writeFileSync(hostPath[process.platform], "");
}

function config(set = false) {
    if (set) {
        fs.writeFileSync("./config", JSON.stringify(hostData));
    } else {
        try {
            fs.accessSync("./config");
            hostData = JSON.parse(fs.readFileSync("./config", "utf-8"));
        } catch (e) {
            fs.writeFileSync("./config", JSON.stringify(hostData));
            hostData = JSON.parse(fs.readFileSync("./config", "utf-8"));
        }
    }
}

function createWindow() {
    Menu.setApplicationMenu(null);
    const mainWindow = new BrowserWindow({
        width: 1000,
        height: 600,
        minWidth: 800,
        minHeight: 400,
        icon: "./H.ico",
        webPreferences: {
            preload: path.join(__dirname, "preload.js"),
            nodeIntegration: true,
        },
    });
    mainWindow.loadFile("index.html");
    // mainWindow.webContents.openDevTools();
}

app.whenReady().then(() => {
    config();
    createWindow();
    ipcMain.handle("getHostData", async (event, ...args) => {
        return new Promise((reslove) => {
            reslove(hostData);
        });
    });
    ipcMain.handle("setHost", async (event, ...args) => {
        changeHost(JSON.parse(args));
    });
    // globalShortcut.register("Alt+CommandOrControl+Enter", () => {
    //     console.log("all");
    // });
    app.on("activate", function () {
        if (BrowserWindow.getAllWindows().length === 0) createWindow();
    });
});

app.on("window-all-closed", function () {
    config(true);
    if (process.platform !== "darwin") app.quit();
});
